using System; 
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WozLib.UI.Ink;

namespace WozLib.UI.Controls
{
  /// <summary>
  /// Interaction logic for ToolSelectToolbar.xaml
  /// </summary>

  public partial class ToolSelectToolbar : System.Windows.Controls.ToolBar
  {
    public List<SketchPad.InkTools> UserToolsEnabledCollection
    {
      get { return _UserToolsEnabledCollection; }
      set 
      { 
        _UserToolsEnabledCollection = value;

        UpdateToolVisibility();
      }
    }
    private List<SketchPad.InkTools> _UserToolsEnabledCollection =
      new List<SketchPad.InkTools>(new SketchPad.InkTools[] 
        {  
          SketchPad.InkTools.Button,
          SketchPad.InkTools.Ellipse,
          SketchPad.InkTools.Eraser,
          SketchPad.InkTools.Gesture,
          SketchPad.InkTools.Gradient,
          SketchPad.InkTools.Image,
          SketchPad.InkTools.Line,
          SketchPad.InkTools.Pencil,
          SketchPad.InkTools.Rectangle,
          SketchPad.InkTools.Selector,
          SketchPad.InkTools.Text
        });

    private void UpdateToolVisibility()
    {
      ClearVisibility();
      foreach (SketchPad.InkTools inkTool in UserToolsEnabledCollection)
      {
        switch (inkTool)
        {
          case SketchPad.InkTools.Pencil:
            Show(_Pencil_Checked);
            break;
          case SketchPad.InkTools.Eraser:
            Show(_Eraser_Checked);
            break;
          case SketchPad.InkTools.Selector:
            Show(_Selector_Checked);
            break;
          case SketchPad.InkTools.Line:
            Show(_Line_Checked);
            break;
          case SketchPad.InkTools.Rectangle:
            Show(_Rectangle_Checked);
            break;
          case SketchPad.InkTools.Ellipse:
            Show(_Ellipse_Checked);
            break;
          case SketchPad.InkTools.Image:
            Show(_Image_Checked);
            break;
          case SketchPad.InkTools.Text:
            Show(_Image_Checked);
            break;
          case SketchPad.InkTools.Button:
            Show(_Button_Checked);
            break;
          case SketchPad.InkTools.Arrow:
            Show(_Arrow_Checked);
            break;
          case SketchPad.InkTools.Gradient:
            Show(_Gradient_Checked);
            break;
          case SketchPad.InkTools.Gesture:
            Show(_Gesture_Checked);
            break;
          case SketchPad.InkTools.Link:
            Show(_Link_Checked);
            break;
          case SketchPad.InkTools.None:
            Show(_None_Checked);
            break;
          default:
            break;
        }
      }

      if (UserToolsEnabledCollection.Count <= 0)
      {
        this.Visibility = Visibility.Hidden;
      }
      else
      {
        this.Visibility = Visibility.Visible;
      }
    }

    private void ClearVisibility()
    {
      Hide(_Arrow_Checked);
      Hide(_Button_Checked);
      Hide(_Ellipse_Checked);
      Hide(_Eraser_Checked);
      Hide(_Gesture_Checked);
      Hide(_Gradient_Checked);
      Hide(_Image_Checked);
      Hide(_Line_Checked);
      Hide(_None_Checked);
      Hide(_Pencil_Checked);
      Hide(_Rectangle_Checked);
      Hide(_Selector_Checked);
      Hide(_Text_Checked);
      Hide(_Link_Checked);
    }

    private void Hide(RadioButton radioButton)
    {
      if (radioButton != null)
      {
        radioButton.Visibility = Visibility.Collapsed;
      }
    }

    private void Show(RadioButton radioButton)
    {
      if (radioButton != null)
      {
        radioButton.Visibility = Visibility.Visible;
      }
    }

    public ToolSelectToolbar()
    {
      InitializeComponent();

      this.Loaded += new RoutedEventHandler(ToolSelectToolbar_Loaded);
    }

    void ToolSelectToolbar_Loaded(object sender, RoutedEventArgs e)
    {
      SketchPad.CurrentInkToolChanged += new SketchPad.CurrentInkToolChangedDelegate(SketchPad_CurrentInkToolChanged);
    }

    void Pencil_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Pencil);
    }

    void Eraser_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Eraser);
    }

    void Selector_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Selector);
    }

    void Rectangle_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Rectangle);
    }

    void Ellipse_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Ellipse);
    }

    void Image_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Image);
    }

    void Gradient_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Gradient);
    }

    void Text_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Text);
    }

    void Arrow_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Arrow);
    }

    void Line_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Line);
    }

    void Button_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Button);
    }

    void Gesture_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Gesture);
    }

    void Link_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.Link);
    }

    void None_Checked(object sender, RoutedEventArgs e)
    {
      SelectTool(SketchPad.InkTools.None);
    }

    /// <summary>
    /// Called when all SketchPads change tools
    /// </summary>
    /// <param name="currentInkTool">the tool all sketch pads are now using</param>
    void SketchPad_CurrentInkToolChanged(SketchPad.InkTools currentInkTool)
    {
      switch (currentInkTool)
      {
        case SketchPad.InkTools.Pencil:
          _Pencil_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.Eraser:
          _Eraser_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.Selector:
          _Selector_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.Rectangle:
          _Rectangle_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.Ellipse:
          _Ellipse_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.Image:
          _Image_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.Gradient:
          _Gradient_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.Text:
          _Text_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.Arrow:
          _Arrow_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.Line:
          _Line_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.Button:
          _Button_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.Gesture:
          _Gesture_Checked.IsChecked = true;
          break;
        case SketchPad.InkTools.None:
          _None_Checked.IsChecked = true;
          break;
        default:
          break;
      }
    }

    public delegate void WozServerCommandOccuredDelegate(WozLib.WozCommands.WozCommand wozServerCommand);

    public event WozServerCommandOccuredDelegate WozServerCommandOccured;

    public delegate void SelectedToolChangedHandler(SketchPad.InkTools tool);
    public event SelectedToolChangedHandler SelectedToolChanged;

    public void SelectTool(SketchPad.InkTools tool)
    {
      if (SelectedToolChanged != null)
      {
        SelectedToolChanged(tool);
      }

      WozLib.WozCommands.SelectToolWozCommand wozSelectToolCommand = new WozLib.WozCommands.SelectToolWozCommand(SketchPad.CurrentInkTool, tool);

      wozSelectToolCommand.Redo();

      if (WozServerCommandOccured != null)
      {
        WozServerCommandOccured(wozSelectToolCommand);
      }
    }

  }
}